<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!--header-->
	
	<jsp:include page="../include/header.jsp"></jsp:include>
	<!--/head-->
	<header id="header">
	<!--header-->
	
	<!--/header-middle-->
	<jsp:include page="../include/header_middle.jsp"></jsp:include>		
	
	<!--header-bottom-->
	<jsp:include page="../include/header_bottom.jsp"></jsp:include>	
	<!--/header-bottom-->
	
	</header>
	


<%

String sid = (String)session.getAttribute("sid");

	
if(sid == null){ //세션이 안잡혀있을 때
	response.sendRedirect("/ecom/member/loginform");
}

int count=0;
/*
String countStr=request.getParameter("count");
if(countStr==null || countStr.equals("")){
	count=1;
}else{
	count=Integer.parseInt(request.getParameter("count").trim());
	if(count==0){
		count=1;
	}
}
request.setAttribute("count", count);
*/


%>
<script language="JavaScript">
var namex="";
function order() {
	f.action = "/ecom/orders/home";
	f.submit();
}







function fncAdd(num,x)
{
    var cur = Number(document.getElementsByName(x)[0].value) + num;
  
    if(cur<1) cur = 1;
    document.getElementsByName(x)[0].value = cur;
    document.getElementsByName("cart_total_pr")[0].value=cur;
}

/*
//선택 항목 구매
function selected_all() {
	f.action = "zzim_selected_all_action.jsp";
	f.submit();
}
*/




</script>

<script type="text/javascript">

$(function(){
	
	$.ajax({
		
		url:'cartformJSON',
	    type:'GET',
	   
	    dataType:'jSON',
		success:function(cartJSON){
			//var cartJSON = eval(data);

			
			var i=0;
			var total=0;
			var html="";
			for(i in cartJSON){
			total=total+cartJSON[i].product_price*cartJSON[i].cart_count;
			
			
			html+="<table class=\"table table-condensed\" align='center'>";
			
			html+="<colgroup>";
			html+="<col width='10%'>";
			html+="<col width='20%'>";
			html+="<col width='15%'>";
			html+="<col width='30%'>";
			html+="<col width='10%'>";
			html+="<col width='*'>";
			html+="</colgroup>";
			
			html+="<tr align='center'>";
			//선택물품 구매 체크박스..
			//html+="<td><input type='checkbox' value='"+cartJSON.cart_no+"' name='sno[]'></td>";
			
			//이미지부분
			
			html+="<td class=\"cart_product\" style='margin: 0 0;display:block;'>";
			html+="<a href='cart_product_Detail?productno="+cartJSON[i].cart_product+"'>"+"<img width='100%' alt='Fujifilm FinePix S2950 Digital Camera' src='/ecom/resources/images/product/"+cartJSON[i].product_image0+"' alt=''></a>";
			html+="<input class='cartno' type='hidden' value='"+cartJSON[i].cart_no+"'>";
			html+="<input class='productno' type='hidden' value='"+cartJSON[i].cart_product+"'>";
			html+="<input class='cartmember' type='hidden' value='"+cartJSON[i].cart_member+"'>";
			html+="</td>"
			
			//이름
			html+="<td class=\"cart_description\">";
			html+="<h4><a href='cart_product_Detail?productno="+cartJSON[i].cart_product+"'>"+cartJSON[i].product_name+"</a></h4>";
			html+="</td>";
			
			//수량
			html+="<td class=\"cart_count\" >";
			html+="<input class='cartcount' type='text' name='countx"+cartJSON[i].cart_no+"' value="+cartJSON[i].cart_count+" size='3' readonly>";
			html+="<img class='update_cart_ii'src='http://wstatic.naver.com/w9/btn_pre9.gif' onClick=\"javascript:fncAdd(-1,'countx"+cartJSON[i].cart_no+"');\">";
			html+="<img class='update_cart_i'src='http://wstatic.naver.com/w9/btn_next9.gif' onClick=\"javascript:fncAdd(1,'countx"+cartJSON[i].cart_no+"');\");'>";
			html+="</td>";
			
			//가격
			html+="<td class=\"cart_price\">";
			html+="<h4>"+cartJSON[i].product_price+"원</h4>";
			html+="</td>";

			//총가격
			
			
			html+="<td id='total_td"+cartJSON[i].cart_no+"' class=\"cart_total\">";
			html+="<h4 id='cart_total_table' class=\"cart_total_pr\">"+cartJSON[i].product_price*cartJSON[i].cart_count+"원</h4>";
			html+="</td>";
			
			//삭제
			html+="<td class=\"cart_delete\" style='margin-right:0;'><a href='#'>";
		
			//html+="<i class=\"fa fa-times\"></i>";
			//html+="<button type='button' class='btn btn-warning btn-l'>삭제</button>";
			html+="삭제";
			html+="</a></td>";
			html+="</tr>";
			html+="</table>";
			};
			$('#aaaa').html(html);
			$('#total_list').html("<h2>총액: "+total+"원</h2>");
			
			//////// 클릭 수정이벤트 시작///////////
			$(document).on('click','.cart_count',function(){
				var cart_count =$(this).parent().find('.cartcount').val();
				var cart_no=$(this).parent().find('.cartno').val();
				//var product_no=$(this).parent().find('.productno').val();
				//var cart_no=h3Element.attr('cart_total');
				
				//alert(cart_count);
				//alert(cart_no);
				
					$.ajax(
						{
							
							url:'update_cart',
						    type:'GET',
						    async:true,
						    data:{
						    	cart_no:cart_no,
						    	cart_count:cart_count
						    },
						    dataType:'json',
							success:function(cartJSON){
								
								//var jQueryDocument=$(xmlData);
								var html="";
								
								html+="<h4  class=\"cart_all\">"+cartJSON+"원</h4>";
											
								$('#total_td'+cart_no).html(html);
								//////물품 각자 총액 계산 끝//////
								
								/*
								jQueryDocument.find('total')
								.each(function(index,carttotal){
									var cart_total=$(carttotal);
									var total=cart_total.find('cart_total').text();
									
									html+="<h3>총액: "+total+"원</h3>"
									
								});
								$('#cart_list_total').html(html);		
							*/
								
								
							
							},
							error:function(){
								
							},
							beforeSend:function(){},
							complete:function(){}
						});
				
					///////////// 리스트 전체 총액 계산 ///////////
					$.ajax(
							{
								url:'total_list_cart',
							    type:'GET',
							   
							    data:{
							    	cart_no:cart_no
							    },
							    dataType:'xml',
								success:function(xmlData){
									
									var jQueryDocument=$(xmlData);
									var html="";
									
									jQueryDocument
									.find('total')
									.each(function(index,carttotal){
									
										var cart_total=$(carttotal);
										var total=cart_total.find('cart_total').text();
										
										html+="<h2>총액: "+total+"원</h2>"
										
									});
									$('#total_list').html(html);		
								
									
									
									
								},
								error:function(){
									//alert("뻑킹 총액구하기 실패");
								},
								beforeSend:function(){},
								complete:function(){}
							});
					//////////리스트 전체 가격 구하기 끗///////////
					
				
			});
			///////// 클릭 수정이벤트 끗 /////////
			
			///////// 클릭 삭제이벤트 시작 /////////
	$(document).on('click','.cart_delete',function(){
		
		var cart_no=$(this).parent().find('.cartno').val();
		
		$.ajax({
			url:"delete_cart", //delete_cart를 맵핑
			type:"get",
			
			data:{
				cart_no:cart_no //cart_no값을 실어 보낸다
			},
			dataType:"json", //받는타입 json
			
			success:function(cartJSON){
				
				var i=0;
				var total=0;
				var html="";
				for(i in cartJSON){
				total=total+cartJSON[i].product_price*cartJSON[i].cart_count;
				
				
				html+="<table class=\"table table-condensed\" align='center'>";
				
				html+="<colgroup>";
				html+="<col width='10%'>";
				html+="<col width='20%'>";
				html+="<col width='15%'>";
				html+="<col width='30%'>";
				html+="<col width='10%'>";
				html+="<col width='*'>";
				html+="</colgroup>";
				
				html+="<thead>";
				html+="<tr align='center'>";
				//선택물품 구매 체크박스..
				//html+="<td><input type='checkbox' value='"+cartJSON.cart_no+"' name='sno[]'></td>";
				
				//이미지부분
				
				html+="<td class=\"cart_product\"  style='margin: 0 0;display:block;'>";
				html+="<a href='cart_product_Detail?productno="+cartJSON[i].cart_product+"'>"+"<img width='100%' alt='Fujifilm FinePix S2950 Digital Camera' src='/ecom/resources/images/product/"+cartJSON[i].product_image0+"' alt=''></a>";
				html+="<input class='cartno' type='hidden' value='"+cartJSON[i].cart_no+"'>";
				html+="<input class='productno' type='hidden' value='"+cartJSON[i].cart_product+"'>";
				html+="<input class='cartmember' type='hidden' value='"+cartJSON[i].cart_member+"'>";
				html+="</td>"
				
				//이름
				html+="<td class=\"cart_description\" >";
				html+="<h4><a href='cart_product_Detail?productno="+cartJSON[i].cart_product+"'>"+cartJSON[i].product_name+"</a></h4>";
				html+="</td>";
				
				//수량
				html+="<td class=\"cart_count\" >";
				html+="<input class='cartcount' type='text' name='countx"+cartJSON[i].cart_no+"' value="+cartJSON[i].cart_count+" size='3' readonly>";
				html+="<img class='update_cart_ii'src='http://wstatic.naver.com/w9/btn_pre9.gif' onClick=\"javascript:fncAdd(-1,'countx"+cartJSON[i].cart_no+"');\">";
				html+="<img class='update_cart_i'src='http://wstatic.naver.com/w9/btn_next9.gif' onClick=\"javascript:fncAdd(1,'countx"+cartJSON[i].cart_no+"');\");'>";
				html+="</td>";
				
				//가격
				html+="<td class=\"cart_price\" >";
				html+="<h4>"+cartJSON[i].product_price+"원</h4>";
				html+="</td>";

				//총가격
				
				
				html+="<td id='total_td"+cartJSON[i].cart_no+"' class=\"cart_total\" >";
				html+="<h4 id='cart_total_table' class=\"cart_total_pr\">"+cartJSON[i].product_price*cartJSON[i].cart_count+"원</h4>";
				html+="</td>";
				
				//삭제
				html+="<td class=\"cart_delete\" style='margin-right:0;'><a href='#'>";
				//html+="<i class=\"fa fa-times\"></i>";
				//html+="<button type='button' class='btn btn-warning btn-l'>삭제</button>";
				html+="삭제";
				html+="</a></td>";
				html+="</tr>";
				html+="</thead>";
				html+="</table>";
				};
				
				$('#aaaa').html(html);
				$('#total_list').html("<h2>총액: "+total+"원</h2>");
				
			}
			});
	});
		//////////// 카트 삭제 이벤트 끗 /////////////
			
			
			
			
		},error:function(){
			alert("카트폼 리스트 실행 실패");
		},
		beforeSend:function(){},
		complete:function(){}
	});

	/*
	var total=0;
	$.getJSON('getcartJSON',
				{member_no:"member_no"},
				function(cartJSONArray){
					var html="";
					
					
					
					$.each(cartJSONArray,function(index,cartJSON){
						total=total+cartJSON.product_price*cartJSON.cart_count;
						
						
						html+="<table class=\"table table-condensed\" align='center' style='TABLE-LAYOUT: fixed'>";
						
						html+="<tr>";
						//선택물품 구매 체크박스..
						//html+="<td><input type='checkbox' value='"+cartJSON.cart_no+"' name='sno[]'></td>";
						
						//이미지부분
						
						html+="<td class=\"cart_product\" width='70' >";
						html+="<a href='cart_product_Detail?productno="+cartJSON.product_no+"'>"+"<center><img width='200%' alt='Fujifilm FinePix S2950 Digital Camera' src='/ecom/resources/images/product/"+cartJSON.product_image+"' alt=''></center></a>";
						html+="<input class='cartno' type='hidden' value='"+cartJSON.cart_no+"'>";
						html+="<input class='productno' type='hidden' value='"+cartJSON.product_no+"'>";
						html+="<input class='cartmember' type='hidden' value='"+cartJSON.cart_member+"'>";
						html+="</td>"
						
						//이름
						html+="<td class=\"cart_description\" width='400'>";
						html+="<center><h4><a href='cart_product_Detail?productno="+cartJSON.product_no+"'>"+cartJSON.product_name+"</a></h4></center>";
						html+="</td>";
						
						//수량
						html+="<td class=\"cart_count\" width='100'>";
						html+="<input class='cartcount' type='text' name='countx"+index+"' value="+cartJSON.cart_count+" size='3' readonly>";
						//html+="<input type='hidden' name='countx"+index+"' value='"+cartJSON.cart_no+"'>";
						//html+="<input type='hidden' name='index'"+index" value='"+index+"'>";
						html+="<img class='update_cart_ii'src='http://wstatic.naver.com/w9/btn_pre9.gif' onClick=\"javascript:fncAdd(-1,'countx"+index+"');\">";
						//html+="<input type='button' onClick='javascript:fncAdd(-1);'>";
						html+="<img class='update_cart_i'src='http://wstatic.naver.com/w9/btn_next9.gif' onClick=\"javascript:fncAdd(1,'countx"+index+"');\");'>";
						//html+="<center><h4>"+cartJSON.cart_count+"</h4></center>";
						
						html+="</td>";
						
						//가격
						html+="<td class=\"cart_price\" width='15%'>";
						html+="<center><h4>"+cartJSON.product_price+"원</h4></center>";
						html+="</td>";
	
						//총가격
						
						
						html+="<td id='total_td"+cartJSON.cart_no+"' class=\"cart_total\" width='200'>";
						html+="<h4 id='cart_total_table' class=\"cart_total_pr\"><center>"+cartJSON.product_price*cartJSON.cart_count+"원</center></h4>";
						html+="</td>";
						
						//삭제
						html+="<td class=\"cart_delete\" width='150'>";
						html+="<center><a class=\"cart_quantity_delete\" href='delete_cart?product_no="+cartJSON.cart_no+"'><i class=\"fa fa-times\"></i></a></center>";
						html+="</td>";
						html+="</tr>";
						
						
						
						
						html+="</table>";

					});
					$('#aaaa').html(html);
					$('#total_list').html("<h2>총액: "+total+"원</h2>");
					
					//////// 클릭 수정이벤트 시작///////////
					$(document).on('click','.cart_count',function(){
						var cart_count =$(this).parent().find('.cartcount').val();
						var cart_no=$(this).parent().find('.cartno').val();
						//var product_no=$(this).parent().find('.productno').val();
						//var cart_no=h3Element.attr('cart_total');
						
						//alert(cart_count);
						//alert(cart_no);
						
							$.ajax(
								{
									
									url:'update_cart',
								    type:'GET',
								    async:true,
								    data:{
								    	cart_no:cart_no,
								    	cart_count:cart_count
								    },
								    dataType:'xml',
									success:function(xmlData){
										
										var jQueryDocument=$(xmlData);
										var html="";
										jQueryDocument
											.find('cart')
												.each(function(index,cartElement){
													
													var $cart =$(cartElement);
													var total = $cart.find('total').text();
													var cart_no=$cart.find('cart_no').text();
													
													
													html+="<h4  class=\"cart_all\"><center>"+total+"원</center></h4>";
													
												});
										
										$('#total_td'+cart_no).html(html);
										//////물품 각자 총액 계산 끝//////
										
										/*
										jQueryDocument.find('total')
										.each(function(index,carttotal){
											var cart_total=$(carttotal);
											var total=cart_total.find('cart_total').text();
											
											html+="<h3>총액: "+total+"원</h3>"
											
										});
										$('#cart_list_total').html(html);		
									*/
										
									/*	
										
									},
									error:function(){
										
									},
									beforeSend:function(){},
									complete:function(){}
								});
							///////////// 리스트 전체 총액 계산 ///////////
							$.ajax(
									{
										url:'total_list_cart',
									    type:'GET',
									   
									    data:{
									    	cart_no:cart_no
									    },
									    dataType:'xml',
										success:function(xmlData){
											
											var jQueryDocument=$(xmlData);
											var html="";
											
											jQueryDocument
											.find('total')
											.each(function(index,carttotal){
											
												var cart_total=$(carttotal);
												var total=cart_total.find('cart_total').text();
												
												html+="<h2>총액: "+total+"원</h2>"
												
											});
											$('#total_list').html(html);		
										
											
											
											
										},
										error:function(){
											//alert("뻑킹 총액구하기 실패");
										},
										beforeSend:function(){},
										complete:function(){}
									});
							//////////리스트 전체 가격 구하기 끗///////////
							
						
					});
					///////// 클릭 수정이벤트 끗 /////////
					
				});
				*/
	
});


</script>



	<form name="f">
	<section id="cart_items">
	
	
		<div class="container">
		
		
			<div class="breadcrumbs">
				<ol class="breadcrumb">
				  
				  <li class="active">장바구니</li>
				</ol>
			</div>
			<!-- 카트 목록 테이블 입니다... -->
			<div class="table-responsive cart_info">
			<table class="table table-condensed"  align='center'>
				<colgroup>
					<col width="10%">
					<col width="20%">
					<col width="15%">
					<col width="30%">
					<col width="10%">
					<col width="*">
				</colgroup>
						<thead>
						<tr class="cart_menu" align="center">
<!-- 						<td class="image" width='20%'>아이템</td> -->
<!-- 						<td class="description" width='50%'>이름</td> -->
<!-- 						<td class="quantity" width='20%'>수량</td> -->
<!-- 						<td class="price" width='20%'>가격</td> -->
<!-- 						<td class="total" width='30%'>총가격</td> -->
<!-- 						<td class="delete" width='20%'>삭제</td> -->
						<td class="image">아이템</td>
						<td class="description">이름</td>
						<td class="quantity">수량</td>
						<td class="price">가격</td>
						<td class="total">총가격</td>
						<td class="delete">삭제</td>
						</tr>
						</thead>
						</table>
						<div id="aaaa"></div>
						<div id="total"></div>
		<table>	
		<tr align="center">
			<td id="total_list" width="90%" style="padding-left:30%">
		</td>
		<td width='0%' type="hidden">							
		<input type="button"  class="btn btn-warning btn-l" value="주문하기" onClick="order();"></input>
		</td>
		</tr>
			</table>
			</div>
		</div>
		
		
	</section> 
	<!--/#cart_items-->
<!--slider-->	
		
	<!--/slider-->

	</form>
	<!--/#do_action-->
	
	<!--Footer-->
	<jsp:include page="../include/footer.jsp"></jsp:include>	
	<!--/Footer-->

